let App = getApp();
var   wxParse = require("../../wxParse/wxParse.js"); 
function getRandomColor() {
  const rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length === 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}

Page(
  {
  inputValue:'',
  data: {
    // banner轮播组件属性
    indicatorDots: true, // 是否显示面板指示点	
    autoplay: true, // 是否自动切换
    interval: 2000,
    duration: 500,
    imgHeights: {}, // 图片的高度
    imgCurrent: {}, // 当前banne所在滑块指针
    indicatorDots: true,
    vertical: false,
    background: [],
    // 页面元素
    items: {},
    newest: {},
    best: {},
    config:{},
    scrollTop: 0,
  },

  onLoad: function() {

    
    this.getbanner();
    this.getIndexData();
  },
  onReady() {
    this.videoContext = wx.createVideoContext('myVideo')
  },
  /**
   * 获取首页数据
   */
  getIndexData: function() {
    let _this = this;
    App._post_form('wxapi/config', {}, function(result) {
      _this.setData({config:result.data});
      wxParse.wxParse('content', 'html', result.data.content, _this, 0);
    });
  },
   /**
   * 获取首页数据
   */
  getbanner: function() {
    let _this = this;
    App._post_form('wxapi/banner/list', {}, function(result) {
       if(result.status=="200"){
        _this.setData({background: result.data});
       }
  
    });
  },

  /**
   * 计算图片高度
   */
  imagesHeight: function(e) {
    let imgId = e.target.dataset.id,
      itemKey = e.target.dataset.itemKey,
      ratio = e.detail.width / e.detail.height, // 宽高比
      viewHeight = 750 / ratio, // 计算的高度值
      imgHeights = this.data.imgHeights;

    // 把每一张图片的对应的高度记录到数组里
    if (typeof imgHeights[itemKey] === 'undefined') {
      imgHeights[itemKey] = {};
    }
    imgHeights[itemKey][imgId] = viewHeight;
    // 第一种方式
    let imgCurrent = this.data.imgCurrent;
    if (typeof imgCurrent[itemKey] === 'undefined') {
      imgCurrent[itemKey] = Object.keys(this.data.items[itemKey].data)[0];
    }
    this.setData({
      imgHeights,
      imgCurrent
    });
  },

  bindChange: function(e) {
    let itemKey = e.target.dataset.itemKey,
      imgCurrent = this.data.imgCurrent;
    // imgCurrent[itemKey] = e.detail.current;
    imgCurrent[itemKey] = e.detail.currentItemId;
    this.setData({
      imgCurrent
    });
  },

  goTop: function(t) {
    this.setData({
      scrollTop: 0
    });
  },

  scroll: function(t) {
    this.setData({
      indexSearch: t.detail.scrollTop
    }), t.detail.scrollTop > 300 ? this.setData({
      floorstatus: !0
    }) : this.setData({
      floorstatus: !1
    });
  },

  onShareAppMessage: function () {
   
  },

  //用户点击右上角分享朋友圈
  onShareTimeline: function () {
  
  },
  bindSendDanmu() {
    this.videoContext.sendDanmu({
      text:this.inputValue,
      color: getRandomColor()
    })
  },
  bindInputBlur(e) {
    this.inputValue = e.detail.value
  },

});